package com.jiyici.network;

import jiyici.Constant.AuthenticateKey;
import jiyici.Constant.ModelColumn;
import jiyici.Constant.PostAction;
import jiyici.Constant.ResponseSpec;
import jiyici.Server2ClientSyncData;

import org.json.JSONObject;

import com.jiyici.controller.DataManager;
import com.jiyici.model.Item;
import com.jiyici.model.ModelUtil;
import com.jiyici.model.User;
import com.jiyici.utils.LogUtil;

public class PostRequest {
    
    private static final String TAG = "PostRequest";

    public static PostRequest requestForRegister(String email, String userName, String password) {
        JSONObject json = new JSONObject();
        try {
            json.put(ModelColumn.USER_COLUMN_EMAIL, email);
            json.put(ModelColumn.USER_COLUMN_USERNAME, userName);
            json.put(ModelColumn.USER_COLUMN_PASSWORD, password);
        } catch (Exception e) {
            LogUtil.error(TAG, "", e);
        }
        return new PostRequest(PostAction.REGISTER, json);
    }

    public static PostRequest requestForLogin(String email, String password) {
        JSONObject json = new JSONObject();
        try {
            json.put(ModelColumn.USER_COLUMN_EMAIL, email);
            json.put(ModelColumn.USER_COLUMN_PASSWORD, password);
        } catch (Exception e) {
            LogUtil.error(TAG, "", e);
        }
        return new PostRequest(PostAction.LOGIN, json);
    }
    
    public static PostRequest requestForServer2ClientSync(Server2ClientSyncData syncData) {
        JSONObject json = authenticateData();
        try {
            json.put(ResponseSpec.KEY_DATA, syncData.toJson());
        } catch (Exception e) {
            LogUtil.error(TAG, "", e);
        }
        return new PostRequest(PostAction.SYNC_SERVER_TO_CLIENT, json);
    }
    
    public static PostRequest requestForClient2ServerSync(JSONObject data) {
        JSONObject json = authenticateData();
        try {
            json.put(ResponseSpec.KEY_DATA, data);
        } catch (Exception e) {
            LogUtil.error(TAG, "", e);
        }
        return new PostRequest(PostAction.SYNC_CLIENT_TO_SERVER, json);
    }

    public static PostRequest requestForSaveItem(Item item) {
        JSONObject json = authenticateData();
        try {
            json.put(ResponseSpec.KEY_DATA, ModelUtil.jsonOf(item));
        } catch (Exception e) {
            LogUtil.error(TAG, "", e);
        }
        return new PostRequest(PostAction.ADD_ITEM, json);
    }

    private static JSONObject authenticateData() {
        User user = DataManager.getUser();
        JSONObject json = new JSONObject();
        try {
            json.put(AuthenticateKey.USER_ID, (long) user.id);
            json.put(AuthenticateKey.TOKEN, user.token);
        } catch (Exception e) {
            LogUtil.error(TAG, "", e);
        }
        return json;
    }

    public int getAction() {
        return mAction;
    }

    public JSONObject getRequestData() {
        return mPostData;
    }

    private PostRequest(int action, JSONObject postData) {
        this.mAction = action;
        this.mPostData = postData;
    }

    private int mAction;

    private JSONObject mPostData;

}
